package com.example.pokemon_chat_room;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.adapter.GMShopAdapter;
import com.example.pokemon_chat_room.adapter.PokeDexAdapter;
import com.example.pokemon_chat_room.adapter.PokeShopAdapter;
import com.example.pokemon_chat_room.pojo.Items;
import com.example.pokemon_chat_room.pojo.Pokemon;
import com.example.pokemon_chat_room.pojo.TradeDto;
import com.example.pokemon_chat_room.request.OkHttp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Shop extends AppCompatActivity {

    private RecyclerView shop_items_list;

    private RecyclerView shop_pokemon_list;

    private List<Items> itemsList = new ArrayList<>();

    private List<TradeDto> tradeDtoList = new ArrayList<>();

    private GMShopAdapter gmShopAdapter;

    private PokeShopAdapter pokeShopAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);


        String menuTitle = "商店";
        androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.title_layout);//设置标题样式
            TextView textView = (TextView) actionBar.getCustomView().findViewById(R.id.display_title);//获取标题布局的textview
            textView.setText(menuTitle);//设置标题名称，menuTitle为String字符串
            actionBar.setHomeButtonEnabled(true);//设置左上角的图标是否可以点击
            actionBar.setDisplayHomeAsUpEnabled(true);//给左上角图标的左边加上一个返回的图标
            actionBar.setDisplayShowCustomEnabled(true);// 使自定义的普通View能在title栏显示，即actionBar.setCustomView能起作用

        }

        shop_items_list = findViewById(R.id.shop_items_list);
        shop_pokemon_list = findViewById(R.id.shop_pokemon_list);


        OkHttp okHttp = new OkHttp();

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 1);
        shop_items_list.setLayoutManager(layoutManager);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = okHttp.Post(getApplicationContext(),"items/getAll", null);
                    System.out.println(jsonObject);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.size(); i++) {
                        Items items = (Items) data.getObject(i, Items.class);
                        itemsList.add(items);
                    }
                    Shop.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            gmShopAdapter = new GMShopAdapter(Shop.this, itemsList);
                            shop_items_list.setAdapter(gmShopAdapter);
//                            shop_items_list.scrollToPosition(0);
                        }
                    });

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();



        RecyclerView.LayoutManager layoutManager1 = new GridLayoutManager(this, 1);
        shop_pokemon_list.setLayoutManager(layoutManager1);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = new JSONObject();
                    SharedPreferences mShared = getSharedPreferences("user", MODE_PRIVATE);

                    String token = mShared.getString("token","");// 从共享参数中获得名叫name的字符串

                    jsonObject.put("token", token);
                    JSONObject jsonObject1 = okHttp.Post(getApplicationContext(),"trade-list/getList", jsonObject);
                    System.out.println(jsonObject1);
                    JSONArray data = jsonObject1.getJSONArray("data");
                    for (int i = 0; i < data.size(); i++) {
                        TradeDto items = (TradeDto) data.getObject(i, TradeDto.class);
                        tradeDtoList.add(items);
                    }
                    Shop.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pokeShopAdapter = new PokeShopAdapter(Shop.this, tradeDtoList);
                            shop_pokemon_list.setAdapter(pokeShopAdapter);
//                            shop_items_list.scrollToPosition(0);
                        }
                    });

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}